<template>
  <basic-container :btnFlag=true>
    <div class="card-border">
      <card-header :header-data="headerData"></card-header>
      <el-form
        label-width="130px"
        :model="subjectForm"
        style="padding-right: 30px;"
        :rules="subjectInfosrules"
        ref="subjectForm"
      >
        <el-row>
          <el-col :span="16" style="margin-top: 27px;">
            <el-form-item class="form-item" label="规划名称 :" prop="achievementTitle">
              <span>{{achievementTitle}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="4" v-if="subjectForm.achievementId!=0">
            <el-button size="small" type="success" style="margin-left:10px;margin-top: 31px"
                       @click="resultDetailTemp()">规划详情
            </el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item class="form-item" label="课题名称:" prop="title">
              <el-input v-model="subjectForm.title" placeholder="请输入课题名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item class="form-item" label="课题概述 :" prop="description">
              <el-input
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4}"
                placeholder="请输入课题概述"
                v-model="subjectForm.description"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item class="form-item" label="创建人 :" prop="createNickname">
              <span>{{createNickname}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item class="form-item" label="创建时间 :" prop="createTime">
              <span>{{createTime}}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="form-submit">
        <el-button type="success" @click.stop="inclusionPlan()">列入计划</el-button>
        <el-button type="primary" @click.stop="commitList()">编辑保存</el-button>
        <el-button type="info" @click.stop="goBack()">关闭</el-button>
      </div>
    </div>
  </basic-container>
</template>

<script>
  import {subjectRules} from "@/const/infoRules";
  import {editSubject, getSubjectInfo, changeSubjectState} from "@/api/subject";
  import {resultDetail} from "@/api/planning/result";
  import {PROJECT_DIC} from '@/const/dict';

  export default {
    name: "editSubject",
    created() {
      this.putSubjectForm();
    },
    data() {
      return {
        headerData: {
          title: '编辑课题'
        },
        subjectInfosrules: subjectRules,// 表单验证
        subjectForm: {
          title: '',
          description: '',
        },
        achievementTitle: '',
        achievementIdTemp: 0,
        createNickname: '',
        createTime: '',
      }
    },
    methods: {
      async putSubjectForm() {
        await getSubjectInfo(this.$route.params.id).then(response => {
          this.subjectForm.title = response.data.data.title;
          this.subjectForm.description = response.data.data.description;
          this.achievementIdTemp = response.data.data.achievementId;
          this.achievementTitle = response.data.data.achievementTitle;
          this.createNickname = response.data.data.createNickname;
          if (response.data.data.createTime != null) {
            var data = response.data.data.createTime.split(' ');
            this.createTime = data[0];
          }
        });
      },
      goBack() {
        this.$router.go(-1);
      },
      commitList() {
        this.$refs.subjectForm.validate(valid => {
          if (valid) {
            this.$confirm("确定提交修改?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              editSubject(this.$route.params.id, this.subjectForm).then(response => {
                if (response.data.code == 0) {
                  this.$notify({
                    title: "成功",
                    message: "修改成功",
                    type: "success",
                    duration: 2000
                  });
                  this.$router.go(-1);
                }
              })
            })
          }
        })
      },
      resultDetailTemp() {
        let src = `/searchDetail/detail/${this.achievementIdTemp}`;
        this.$router.push({
          path: src
        })
      },
      inclusionPlan(row) {
        this.$refs.subjectForm.validate(valid => {
          if (valid) {
            this.$confirm("确定将此课题列入计划吗?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              editSubject(this.$route.params.id, Object.assign({
                state: '1',
                description: this.subjectForm.description,
                title: this.subjectForm.title,
              })).then(response => {
                if (response.data.code == 0) {
                  this.$notify({
                    title: '成功',
                    message: '列入计划成功',
                    type: 'success',
                    duration: 2000
                  });
                  this.$router.go(-1);
                }
              });
            })
          }
        })

      },
    }
  }
</script>


<style lang="scss" scoped>
  .card-border {
    border: 1px solid #d2d2d2;
    padding: 12px;
  }

  .form-submit {
    text-align: center;
    margin-top: 15px;
  }

  /deep/ .avue-crud__menu {
    min-height: 1px;
  }
</style>
